﻿using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

// Token: 0x02000037 RID: 55
[RequireComponent(typeof(BoxSlider), typeof(RawImage))]
[ExecuteInEditMode]
public class SVBoxSlider : MonoBehaviour
{
	// Token: 0x17000029 RID: 41
	// (get) Token: 0x0600012D RID: 301 RVA: 0x0000CB2B File Offset: 0x0000AD2B
	public RectTransform rectTransform
	{
		get
		{
			return base.transform as RectTransform;
		}
	}

	// Token: 0x0600012E RID: 302 RVA: 0x0000D368 File Offset: 0x0000B568
	private void Awake()
	{
		this.slider = base.GetComponent<BoxSlider>();
		this.image = base.GetComponent<RawImage>();
		if (Application.isPlaying)
		{
			this.supportsComputeShaders = SystemInfo.supportsComputeShaders;
			if (this.overrideComputeShader)
			{
				this.supportsComputeShaders = false;
			}
			if (this.supportsComputeShaders)
			{
				this.InitializeCompute();
			}
			this.RegenerateSVTexture();
		}
	}

	// Token: 0x0600012F RID: 303 RVA: 0x0000D3CC File Offset: 0x0000B5CC
	private void InitializeCompute()
	{
		if (this.renderTexture == null)
		{
			this.renderTexture = new RenderTexture(this.textureWidth, this.textureHeight, 0, RenderTextureFormat.RGB111110Float);
			this.renderTexture.enableRandomWrite = true;
			this.renderTexture.Create();
		}
		this.compute = Resources.Load<ComputeShader>("Shaders/Compute/GenerateSVTexture");
		this.kernelID = this.compute.FindKernel("CSMain");
		this.image.texture = this.renderTexture;
	}

	// Token: 0x06000130 RID: 304 RVA: 0x0000D454 File Offset: 0x0000B654
	private void OnEnable()
	{
		if (Application.isPlaying && this.picker != null)
		{
			this.slider.onValueChanged.AddListener(new UnityAction<float, float>(this.SliderChanged));
			this.picker.onHSVChanged.AddListener(new UnityAction<float, float, float>(this.HSVChanged));
		}
	}

	// Token: 0x06000131 RID: 305 RVA: 0x0000D4B4 File Offset: 0x0000B6B4
	private void OnDisable()
	{
		if (this.picker != null)
		{
			this.slider.onValueChanged.RemoveListener(new UnityAction<float, float>(this.SliderChanged));
			this.picker.onHSVChanged.RemoveListener(new UnityAction<float, float, float>(this.HSVChanged));
		}
	}

	// Token: 0x06000132 RID: 306 RVA: 0x0000D50C File Offset: 0x0000B70C
	private void OnDestroy()
	{
		if (this.image.texture != null)
		{
			if (this.supportsComputeShaders)
			{
				this.renderTexture.Release();
			}
			else
			{
				UnityEngine.Object.DestroyImmediate(this.image.texture);
			}
		}
	}

	// Token: 0x06000133 RID: 307 RVA: 0x0000D55A File Offset: 0x0000B75A
	private void SliderChanged(float saturation, float value)
	{
		if (this.listen)
		{
			this.picker.AssignColor(ColorValues.Saturation, saturation);
			this.picker.AssignColor(ColorValues.Value, value);
		}
		this.listen = true;
	}

	// Token: 0x06000134 RID: 308 RVA: 0x0000D588 File Offset: 0x0000B788
	private void HSVChanged(float h, float s, float v)
	{
		if (!this.lastH.Equals(h))
		{
			this.lastH = h;
			this.RegenerateSVTexture();
		}
		if (!s.Equals(this.slider.normalizedValue))
		{
			this.listen = false;
			this.slider.normalizedValue = s;
		}
		if (!v.Equals(this.slider.normalizedValueY))
		{
			this.listen = false;
			this.slider.normalizedValueY = v;
		}
	}

	// Token: 0x06000135 RID: 309 RVA: 0x0000D608 File Offset: 0x0000B808
	private void RegenerateSVTexture()
	{
		if (this.supportsComputeShaders)
		{
			float val = (!(this.picker != null)) ? 0f : this.picker.H;
			this.compute.SetTexture(this.kernelID, "Texture", this.renderTexture);
			this.compute.SetFloats("TextureSize", new float[]
			{
				(float)this.textureWidth,
				(float)this.textureHeight
			});
			this.compute.SetFloat("Hue", val);
			int threadGroupsX = Mathf.CeilToInt((float)this.textureWidth / 32f);
			int threadGroupsY = Mathf.CeilToInt((float)this.textureHeight / 32f);
			this.compute.Dispatch(this.kernelID, threadGroupsX, threadGroupsY, 1);
		}
		else
		{
			double h = (double)((!(this.picker != null)) ? 0f : (this.picker.H * 360f));
			if (this.image.texture != null)
			{
				UnityEngine.Object.DestroyImmediate(this.image.texture);
			}
			Texture2D texture2D = new Texture2D(this.textureWidth, this.textureHeight);
			texture2D.hideFlags = HideFlags.DontSave;
			for (int i = 0; i < this.textureWidth; i++)
			{
				Color32[] array = new Color32[this.textureHeight];
				for (int j = 0; j < this.textureHeight; j++)
				{
					array[j] = HSVUtil.ConvertHsvToRgb(h, (double)((float)i / 100f), (double)((float)j / 100f), 1f);
				}
				texture2D.SetPixels32(i, 0, 1, this.textureHeight, array);
			}
			texture2D.Apply();
			this.image.texture = texture2D;
		}
	}

	// Token: 0x04000223 RID: 547
	public ColorPicker picker;

	// Token: 0x04000224 RID: 548
	private BoxSlider slider;

	// Token: 0x04000225 RID: 549
	private RawImage image;

	// Token: 0x04000226 RID: 550
	private ComputeShader compute;

	// Token: 0x04000227 RID: 551
	private int kernelID;

	// Token: 0x04000228 RID: 552
	private RenderTexture renderTexture;

	// Token: 0x04000229 RID: 553
	private int textureWidth = 100;

	// Token: 0x0400022A RID: 554
	private int textureHeight = 100;

	// Token: 0x0400022B RID: 555
	private float lastH = -1f;

	// Token: 0x0400022C RID: 556
	private bool listen = true;

	// Token: 0x0400022D RID: 557
	[SerializeField]
	private bool overrideComputeShader;

	// Token: 0x0400022E RID: 558
	private bool supportsComputeShaders;
}
